package com.industrial.app.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;

import com.industrial.app.domain.BizPowerConsumption;
import com.industrial.app.mapper.BizPowerConsumptionFormalMapper;
import com.industrial.app.mapper.BizPowerConsumptionMapper;
import com.industrial.app.service.IBizPowerConsumptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 重点省份与全国工业用电量同比Service业务层处理
 * 
 * @author Zhou_S
 * @date 2023-12-27
 */
@Service
public class BizPowerConsumptionServiceImpl implements IBizPowerConsumptionService
{

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private BizPowerConsumptionMapper bizPowerConsumptionMapper;

    @Autowired
    private BizPowerConsumptionFormalMapper bizPowerConsumptionFormalMapper;
    /**
     * 查询重点省份与全国工业用电量同比
     * 
     * @param baseId 重点省份与全国工业用电量同比主键
     * @return 重点省份与全国工业用电量同比
     */
    @Override
    public BizPowerConsumption selectBizPowerConsumptionByBaseId(String baseId)
    {
        return bizPowerConsumptionMapper.selectBizPowerConsumptionByBaseId(baseId);
    }

    /**
     * 查询重点省份与全国工业用电量同比列表
     * 
     * @param bizPowerConsumption 重点省份与全国工业用电量同比
     * @return 重点省份与全国工业用电量同比
     */
    @Override
    public List<BizPowerConsumption> selectBizPowerConsumptionList(BizPowerConsumption bizPowerConsumption)
    {
        return bizPowerConsumptionMapper.selectBizPowerConsumptionList(bizPowerConsumption);
    }

    /**
     * 新增重点省份与全国工业用电量同比
     * 
     * @param bizPowerConsumption 重点省份与全国工业用电量同比
     * @return 结果
     */
    @Override
    public int insertBizPowerConsumption(BizPowerConsumption bizPowerConsumption)
    {
        bizPowerConsumption.setBaseId(IdUtil.simpleUUID());
        bizPowerConsumption.setBaseCreateTime(new Date());
        DateTime datesTimes= DateUtil.parse(bizPowerConsumption.getBaseMonth(),"yyyy年MM月");
        bizPowerConsumption.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        return bizPowerConsumptionMapper.insertBizPowerConsumption(bizPowerConsumption);
    }

    /**
     * 修改重点省份与全国工业用电量同比
     * 
     * @param bizPowerConsumption 重点省份与全国工业用电量同比
     * @return 结果
     */
    @Override
    public int updateBizPowerConsumption(BizPowerConsumption bizPowerConsumption)
    {
        DateTime datesTimes= DateUtil.parse(bizPowerConsumption.getBaseMonth(),"yyyy年MM月");
        bizPowerConsumption.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        return bizPowerConsumptionMapper.updateBizPowerConsumption(bizPowerConsumption);
    }

    /**
     * 批量删除重点省份与全国工业用电量同比
     * 
     * @param baseIds 需要删除的重点省份与全国工业用电量同比主键
     * @return 结果
     */
    @Override
    public int deleteBizPowerConsumptionByBaseIds(String[] baseIds)
    {
        return bizPowerConsumptionMapper.deleteBizPowerConsumptionByBaseIds(baseIds);
    }

    /**
     * 删除重点省份与全国工业用电量同比信息
     * 
     * @param baseId 重点省份与全国工业用电量同比主键
     * @return 结果
     */
    @Override
    public int deleteBizPowerConsumptionByBaseId(String baseId)
    {
        return bizPowerConsumptionMapper.deleteBizPowerConsumptionByBaseId(baseId);
    }

    /**
     * @return
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizPowerConsumptionMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            List<String> collect = uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
            collect = collect.stream().map(item -> {
                String[] split = item.split("-");
                return split[0] + "年" + split[1] + "月";
            }).collect(Collectors.toList());
            return collect;
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * <p>
     * 清空所有的重点省份与全国工业用电量同比数据
     * </p>
     *
     * @return
     */
    @Override
    public void clearAllTableData() {
        bizPowerConsumptionMapper.clearAllTableData();
    }

    /**
     * @return
     */
    @Override
    public boolean transferDataInOneClick() {
        try{
            List<BizPowerConsumption> result = bizPowerConsumptionMapper.selectBizPowerConsumptionAllData();

            int count = 0;
            List<BizPowerConsumption> list = new ArrayList<>();
            for(BizPowerConsumption item : result){
                list.add(item);
                count++;
                if(count >= 100){
                    bizPowerConsumptionFormalMapper.batchInsertBizPowerConsumptionFormal(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                bizPowerConsumptionFormalMapper.batchInsertBizPowerConsumptionFormal(list);
                list.clear();
            }
            return true;
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return false;
        }
    }
}
